package mms.farina.thecell.activities;

import java.util.ArrayList;

import mms.farina.thecell.R;
import mms.farina.thecell.services.ReceiveService;

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**Activity per la gestione della schermata iniziale della chat*/
public class ChatMain extends ListActivity {

	String[] listItem;
	public static String selectedAlias="";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat_main);

		ArrayList<String> list=new ArrayList<String>();
		
		if(!ReceiveService.convStore.containsKey("BROADCAST"))list.add("BROADCAST");
		
		for(String k:ReceiveService.convStore.keySet())list.add(k+"-Offline");
		
		for(String k: ReceiveService.netMembers.keySet()){
			if(!list.contains(k) && !k.equals(ReceiveService.NAME)){
				if(ReceiveService.activeNodes.keySet().contains(k))list.add(k);
				else list.add(k+"-Offline");
			}
		}
		
		
		listItem=new String[list.size()];
		
		for(int i=0; i<list.size(); i++)listItem[i]=list.get(i);
		
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1,
				listItem
				));
		

		final ListView lv = getListView();
		lv.setItemChecked(0, true);
		lv.setBackgroundColor(Color.WHITE);

	}
	
	@Override 
    public void onListItemClick(ListView l, View v, int position, long id) {
		
        selectedAlias=(String)l.getItemAtPosition(position);
        selectedAlias=selectedAlias.replace("-Offline", "");
		Intent intent = new Intent();
        intent.setClass(this, SendMessage.class);
        startActivity(intent);
    }



}
